//
//  ADVisitorView.swift
//  myBlog
//
//  Created by mac2014 on 16/3/21.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

//定义代理协议
@objc protocol ADVisitorViewDelegate: NSObjectProtocol {
    //定义代理方法
    optional func visitorViewDidClickLoginBtn()
    
    optional func visitorViewDidClickRegisterBtn()
}

class ADVisitorView: UIView {
    //定义代理协议属性
    weak var delegate: ADVisitorViewDelegate?
    
    //代理方法
    //登录按钮的点击事件
    func didClickLoginBtn() {
        delegate?.visitorViewDidClickLoginBtn?()
    }
    
    //注册按钮的点击事件
    func didClickRegisterBtn() {
        delegate?.visitorViewDidClickRegisterBtn?()
    }
    
    override init(frame: CGRect) {
        //只有创建super.init后算创建完控件
        super.init(frame: frame)
        prepareView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    
    //设置访客视图：后面三页调用该方法
    func setupVisitorView(imageName: String, massage: String) {
        rotationImg.hidden = true
        iconView.image = UIImage (named: imageName)
        massageLabel.text = massage
    }
    
    //rotationView旋转设置
    func startRotate() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = M_PI * 2
        rotation.repeatCount = MAXFLOAT
        rotation.duration = 10
        rotation.removedOnCompletion = false
        
        rotationImg.layer.addAnimation(rotation, forKey: "sb")
    }
    
    //mark - prepareView：设置约束
    func prepareView() {
        //添加自控家
        addSubview(rotationImg)
        addSubview(coverView)
        addSubview(iconView)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(massageLabel)
        
        //添加约束
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        massageLabel.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        rotationImg.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        //rotationView
        addConstraint(NSLayoutConstraint(item: rotationImg, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: rotationImg, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        //homeView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: rotationImg, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //massageLabel
        addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: massageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 250))
        
        //registerBtn
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 80))
        
        //loginBtn
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: -100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 80))
        
        //coverView
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    }
    
    
    
    //mark - 懒加载
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加点击事件
        btn.addTarget(self, action: "didClickRegisterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
    }()
    //登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        btn.addTarget(self, action: "didClickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()
    //提示框
    lazy var massageLabel: UILabel = {
       let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(16)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    //图
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //转轮
    lazy var rotationImg: UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩
    lazy var coverView: UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
